package org.tbwork.utility.httprr;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.tbwork.utility.httprr.enums.RequestType;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;  

public class StartCase {
  
private static final int TIMEOUT = 10000;//ms
	
	 private static HttpRR executor = new HttpRR(HttpConfig.custom().autoCookieRecord(false).setConnectionRequestTimeout(TIMEOUT).setConnectTimeout(TIMEOUT).setSocketTimeout(2*TIMEOUT).build(), true, 100);
	
	 private static ExecutorService es = Executors.newCachedThreadPool();
	 
	 private static List<String> titles = new ArrayList<String>();
	 private static List<String> titleUrls = new ArrayList<String>();
	 
	 public static void main(String[] args) throws Exception  
	 {     
		 yipinxianjiao();
	 } 
	 
	 
	 private static void yipinxianjiao() throws Exception{
		 FileWriter writer = new FileWriter("C://大神甩不掉.txt", true); 
		 String mainUrl = "http://www.bxwx.la/b/32/32950/";
		 Request request = new Request(mainUrl);
		 Response response = executor.execute(request);
		 TimeUnit.SECONDS.sleep(1);
		 Document doc = response.getDocument();
		 Elements eles = doc.select("div.box_con div#list dl dd");
		 int count  = 0;
		 for(Element item : eles){
			Element child = item.child(0);
			String title = item.text();
			String link = "http://www.bxwx.la" + child.attr("href");
			titles.add(title);
			titleUrls.add(link); 
			System.out.println("Current title : " + title); 
		    String content = getContent(title, link);
		    writer.write(content);  
		    writer.flush();
			TimeUnit.MILLISECONDS.sleep(200); 
		 }  
	    writer.close();
	 }
	 
	 private static String getContent(String title , String url){
		 try{
			 Request request = new Request(url);
			 Response response = executor.execute(request);
			 Document doc = response.getDocument();
			 Elements eles =  doc.select("div.box_con div#content");
			 if(eles!=null){
				 String html = eles.get(0).text();
				 html = html.replace("<br><br>", "\r\n").replace("<br>", "\r\n");
				 doc = Jsoup.parse(html); 
				 return "\r\n\r\n"+ title +"\r\n\r\n" +doc.text();
			 }
		 }
		 catch(Exception e){
			 executor = null;
			 executor =new HttpRR(HttpConfig.custom().autoCookieRecord(false).setConnectionRequestTimeout(TIMEOUT).setConnectTimeout(TIMEOUT).setSocketTimeout(TIMEOUT).build(), true, 100);
			 return getContent(title, url);
		 }
		 return ""; 
	 }
	 
	 private static String getContentInner(String title ,String url){
		 try{
			 Request request = new Request(url);
			 Response response = executor.execute(request);
			 Document doc = response.getDocument();
			 Elements eles =  doc.select("div.box_con div#content");
			 if(eles!=null){
				 String html = eles.get(0).text();
				 html = html.replace("<br><br>", "\r\n").replace("<br>", "\r\n");
				 doc = Jsoup.parse(html); 
				 return "\r\n\r\n"+ title +"\r\n\r\n" +doc.text();
			 }
			 return "";
		 }
		 catch(Exception e){
			 return "";
		 } 
	 }
}




















